<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <div class="panel-line flex-end" style="margin-bottom: 20px">
            <el-button type="primary" @click="handleAdd" size="mini">+ 新增</el-button>
          </div>
        </div>
        <!--数据信息-->
        <el-table stripe :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="ID" prop="id"></el-table-column>
          <el-table-column align="center" header-align="center" label="角色名称" prop="name"></el-table-column>
          <el-table-column align="center" header-align="center" label="使用人数" prop="use_count">
            <template slot-scope="scope">
              <span class="link" @click="handleUser(scope)">
                {{ scope.row.use_count }}
              </span>
            </template>
          </el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope)">编辑</el-button>
              <el-button type="text" @click="handleDelete(scope)" :class="{ 'button-danger': scope.row.use_count === 0 && scope.row.name !== 'admin' }" :disabled="scope.row.use_count > 0 || scope.row.name === 'admin'">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>

    <!--  新增角色 -->
    <el-drawer :title="this.current.id ? '编辑角色' : '新增角色'" :visible.sync="showDialogAdd" size="50%">
      <DialogAdd v-if="showDialogAdd" :roleList="dataList" :info="current" @close="showDialogAdd = false" @success="handleEditSuccess" />
    </el-drawer>
  </div>
</template>

<script>
import { apiSysRolesList, apiSysRolesDel } from '@/api/system'
import DialogAdd from './components/DialogAdd.vue'

export default {
  name: 'Role',
  components: { DialogAdd },
  data() {
    return {
      current: {},
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
      showDialogAdd: false,
    }
  },
  activated() {
    this.$refs.table.doLayout()
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let info = { pageNumber: this.pageNumber, pageSize: this.pageSize }
      apiSysRolesList(info)
        .then((res) => {
          const { data, total } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 编辑
    handleEdit(scope) {
      this.current = scope.row
      this.showDialogAdd = true
    },
    // 添加
    handleAdd() {
      this.current = {}
      this.showDialogAdd = true
    },
    // 删除
    handleDelete(scope) {
      window.enterPreventDefault = false
      this.$confirm('确定要删除' + scope.row.name + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          window.enterPreventDefault = true
          this.dataListLoading = true
          apiSysRolesDel({ ids: [scope.row.id] })
            .then(() => {
              this.dataListLoading = false
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.getDataList()
            })
            .catch(() => {
              this.dataListLoading = false
            })
        })
        .catch(() => {
          window.enterPreventDefault = true
        })
    },
    handleEditSuccess() {
      this.showDialogAdd = false
      this.getDataList()
    },
    handleUser(scope) {
      this.$store.dispatch('permission/routeAuthorization', 'Account').then((res) => {
        this.$router.push({
          path: '/system/account',
          query: {
            role_id: scope.row.id,
          },
        })
      })
    },
  },
}
</script>
